package com.beadhouse.usermanage.support.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.RoleEntity;
import com.beadhouse.entity.UserEntity;
import com.beadhouse.usermanage.ao.UserAo;
import com.beadhouse.usermanage.dao.UserManageDao;
import com.beadhouse.usermanage.dto.NurListDto;
import com.beadhouse.usermanage.dto.UserModifyDto;
import com.beadhouse.usermanage.service.UserManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserManageServcieImp implements UserManageService{
	@Autowired
	UserManageDao umd;
	
	@Override
	public void addUser(@NotNull UserAo ua,int currentId) throws Exception {
		UserEntity ue=new UserEntity();
		if(umd.exists("account", ua.getAccount())) {
			throw new BeadHouseException("账号已存在");
		}
		ue.setAccount(ua.getAccount());
		ue.setAge(Integer.parseInt(ua.getAge()));
		UserEntity cue=new UserEntity();
		cue.setId(currentId);
		ue.setCreateBy(cue);
		Date date=new Date();
		ue.setCreateTime(date);
		ue.setEmail(null);
		ue.setId(null);
		ue.setIsDeleted(false);
		ue.setStatus("1".equals(ua.getStatus()));
		ue.setNickname(ua.getUsername());
		ue.setPassword("123456");
		ue.setPhoneNumber(null);
		ue.setRemarks(ua.getRemarks());
		RoleEntity role=new RoleEntity();
		role.setId(Integer.parseInt(ua.getRoleId()));
		ue.setRole(role);
		ue.setSex(Integer.parseInt(ua.getSex()));
		ue.setUpdateBy(null);
		ue.setUpdateTime(null);
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		int i=(c.get(Calendar.MONTH)+1);
		int count=umd.userCount()+1;
		if("2".equals(ua.getRoleId())) {
			ue.setUserCode("YS"+c.get(Calendar.YEAR)+((i<10)?("0"+i):i)+c.get(Calendar.DAY_OF_MONTH)+count);
		}
		if("3".equals(ua.getRoleId())) {
			ue.setUserCode("HS"+c.get(Calendar.YEAR)+((i<10)?("0"+i):i)+c.get(Calendar.DAY_OF_MONTH)+count);
		}
		ue.setUsername(ua.getUsername());
		umd.insert(ue);
		
		
	}

	@Override
	public void deleteUser(int id, Map<String, Object> map) throws Exception {
		boolean exists=umd.exists("id",id);
		if(!exists) {
			throw new BeadHouseException("id不存在");
		}
		umd.update(id, map);
		
	}

	@Override
	public void updateUser(int id, Map<String, Object> map) throws Exception {
		
		UserEntity old=umd.select("id",id);
		if(old==null) {
			throw new BeadHouseException("用户不存在");
		}
		Integer sex=old.getSex();
		Integer age=old.getAge();
		String remarks=old.getRemarks();
		Boolean status=old.getStatus();
		if(sex!=null) {
			if(sex.equals(map.get("sex"))) {
				map.remove("sex");
			}
		}
		if(age!=null) {
			if(age.equals(map.get("age"))) {
				map.remove("age");
			}
			
		}
		
		if(remarks!=null) {
			if(remarks.equals(map.get("remarks"))) {
				map.remove("remarks");
			}
		}
		if(status!=null) {
			if(status.equals(map.get("status"))) {
				map.remove("status");
			}
		}
		
		umd.update(id, map);
	}

	@Override
	public QueryResult<UserEntity> selectUser(String key,int pageNo) throws Exception {
		if(key!=null) {
			if(key.length()!=0) {
				key="%"+key+"%";
			}else {
				key=null;
			}
		}
		PageHelper.startPage(pageNo,5);
		List<UserEntity> rows=umd.selectByLike(key);
		QueryResult<UserEntity> result=new QueryResult<UserEntity>();
		PageInfo<UserEntity> page = new PageInfo<>(rows);
		result.setPageNo(1);
		result.setPageSize(5);
		result.setRows(rows);
		result.setTotalRows((int)page.getTotal());
		return result;
	}

	@Override
	public UserModifyDto selectOne(int id) throws Exception {
		boolean exists=umd.exists("id",id);
		if(!exists) {
			throw new BeadHouseException("id不存在");
		}
		UserEntity ue=umd.select("id", id);
		UserModifyDto ud=new UserModifyDto();
		ud.setAge(ue.getAge());
		ud.setRemarks(ue.getRemarks());
		ud.setStatus(ue.getStatus());
		ud.setSex(ue.getSex());
		return ud;
	}

	@Override
	public QueryResult<NurListDto> selectNur(String key,int pageNo) throws Exception {
		if(key!=null) {
			if(key.length()!=0) {
				key="%"+key+"%";
			}else {
				key=null;
			}
		}
		PageHelper.startPage(pageNo,5);
		List<UserEntity> list=umd.selectByRoleId(key);
		List<NurListDto> rows=new ArrayList<>();
		for(UserEntity ue:list) {
			NurListDto nld=new NurListDto();
			nld.setPhoneNumber(ue.getPhoneNumber());
			nld.setUsername(ue.getUsername());
			nld.setRemarks(ue.getRemarks());
			nld.setUpdateTime(ue.getUpdateTime());
			nld.setId(ue.getId());
			rows.add(nld);
		}
		QueryResult<NurListDto> result=new QueryResult<NurListDto>();
		PageInfo<UserEntity> page = new PageInfo<>(list);
		result.setPageNo(1);
		result.setPageSize(5);
		result.setRows(rows);
		result.setTotalRows((int)page.getTotal());
		return result;
	}
	
	
}
